package cn.com.selfsoft.smartcity.commons.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.Clock;
import java.time.LocalDateTime;

/**
 * LocalDateTime序列化成时间戳
 *
 *
 */
public class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> {

    public LocalDateTimeSerializer(Class<LocalDateTime> t) {
        super(t);
    }

    @Override
    public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
//        jsonGenerator.writeNumber(Date.from(localDateTime.atZone(Clock.systemDefaultZone().getZone()).toInstant()).getTime());
        jsonGenerator.writeNumber(localDateTime.atZone(Clock.systemDefaultZone().getZone()).toInstant().toEpochMilli());
    }
}
